import Vue from 'vue'
import Router from 'vue-router'
// 引入动态路由模块的规则  权限一直被改 临时放静态路由   salarys attendances social做了权限
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/setting'
import socialRouter from './modules/social'

import userRouter from './modules/user.js' // 放在静态路由 都能访问到的
Vue.use(Router)

/* Layout */
import Layout from '@/layout'

// 静态路由 constantRoutes
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },
  {
    path: '/import',
    component: Layout,
    hidden: true, // 隐藏在左侧菜单中
    children: [{
      path: '', // 二级路由path什么都不写 表示二级默认路由
      component: () => import('@/views/import')
    }]
  },
  departmentsRouter,
  employeesRouter,
  settingRouter,
  permissionRouter,
  approvalsRouter,
  userRouter
  // 404 page must be placed at the end !!!
  // 动态权限控制  404不能放在静态路由里
  // { path: '*', redirect: '/404', hidden: true }

]
// 动态路由 asyncRoutes   export做权限会用到
export const asyncRoutes = [
  // approvalsRouter,
  // departmentsRouter,
  // employeesRouter,
  // permissionRouter,
  // settingRouter,
  attendancesRouter,
  salarysRouter,
  socialRouter
]
const createRouter = () => new Router({
  mode: 'history', // 历史模式  不写为哈希模式
  base: '/hr/', // 配置项目的基础地址
  scrollBehavior: () => ({ y: 0 }), // 管理滚动行为 如果出现滚动 切换就让 让页面回到顶部
  // routes: [...constantRoutes, ...asyncRoutes] // 临时合并所有的路由
  routes: constantRoutes // 权限处理后 动态路由静态路由分离
})

const router = createRouter() // 实例化一个路由

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
// 重置路由
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

export default router
